package osmedile.intellij.checkmyvars;

import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.IconLoader;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author Olivier Smedile
 * @version $Id$
 */
@State(
        name = "CheckMyVars",
        storages = {@Storage(id = "CheckMyVars",
                file = "$OPTIONS$/CheckMyVars.xml")}
)
public class CheckMyVarsAppComp implements ApplicationComponent, Configurable,
        PersistentStateComponent<ConfigImpl> {


    private ConfigImpl config = new ConfigImpl();


    private CMVConfig form;

    public ConfigImpl getConfig() {
        return config;
    }

    @Nls
    public String getDisplayName() {
        return "Check My Vars";
    }


    @Nullable
    public Icon getIcon() {

        return IconLoader.findIcon("wi0126-32.png", this.getClass());
    }


    @Nullable
    @NonNls
    public String getHelpTopic() {
        return null;
    }


    public JComponent createComponent() {
        if (form == null) {
            form = new CMVConfig();
        }
        return form.getRootComponent();
    }


    public boolean isModified() {
        return form != null && form.isModified(this.config);
    }

    public void apply() throws ConfigurationException {
        if (form != null) {
            form.getData(config);
        }
    }

    public void reset() {
        if (form != null) {
            form.setData(this.config);
        }
    }

    public void disposeUIResources() {
        form = null;
    }


    public void initComponent() {
    }


    public void disposeComponent() {
    }


    @NotNull
    public String getComponentName() {
        return "CheckMyVarsAppComp";
    }


    public ConfigImpl getState() {
        return config;
    }


    public void loadState(ConfigImpl state) {
        XmlSerializerUtil.copyBean(state, config);
    }
}
